package cn.edu.ldh.toolapp.stores;

import cn.edu.ldh.toolapp.entity.Merit;
import cn.edu.ldh.toolapp.entity.MeritRes;
import cn.edu.ldh.toolapp.entity.NoteItem;
import cn.edu.ldh.toolapp.util.ResUtil;
import ohos.aafwk.ability.AbilitySlice;
import ohos.data.DatabaseHelper;
import ohos.data.orm.OrmContext;
import ohos.data.orm.OrmDatabase;
import ohos.data.orm.OrmPredicates;
import ohos.data.orm.annotation.Database;
import ohos.data.rdb.RdbOpenCallback;
import ohos.data.rdb.RdbStore;
import ohos.data.rdb.ValuesBucket;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import java.util.List;

@Database(entities = {Merit.class}, version = 1)
public class MeritStore extends OrmDatabase {

    private static final String DATABASE_LINT = "MeritStore";
    private static final String DATABASE_NAME = "MeritStore.db";
    private static final int SUCCESS_CODE = 200;
    private static final int FAIL_CODE = 500;
    private static MeritStore instance;
    private static final HiLogLabel LABEL = new HiLogLabel(HiLog.LOG_APP, 1, "MeritStore");
    static OrmContext ormContext;

    //单例模式
    public static MeritStore getInstance() {
        if(instance == null) {
            instance = new MeritStore();
        }
        return instance;
    }

    //创建数据库并初始化
    public void createDataBase(AbilitySlice slice) {
        HiLog.error(LABEL, "开始创建");
        DatabaseHelper helper = new DatabaseHelper(slice);
        ormContext = helper.getOrmContext(DATABASE_LINT, DATABASE_NAME, MeritStore.class);
        Merit merit = new Merit();
        merit.setMeritNum(0);
        boolean insert = ormContext.insert(merit);
        boolean flush = ormContext.flush();
        if(insert || flush) {
            HiLog.error(LABEL, "数据库初始化成功");
        }
        HiLog.error(LABEL, "数据库创建成功");
    }

    /**
     * 获取功德
     * @return merit;
     * */
    public MeritRes getMerit() {
        if(ormContext == null) {
            HiLog.error(LABEL, "获取失败");
            return ResUtil.backMeritRes(FAIL_CODE, "获取失败", null);
        } else {
            OrmPredicates list = ormContext.where(Merit.class);
            List<Merit> meritList = ormContext.query(list);
            return ResUtil.backMeritRes(SUCCESS_CODE, "获取成功",  meritList.get(0));
        }
    }

    /**
     * 更新功德
     * @param merit;
     * @return message;
     **/
    public MeritRes updateMerit(Merit merit) {
        ValuesBucket valuesBucket = new ValuesBucket();
        OrmPredicates oldMerit = ormContext.where(Merit.class).equalTo("meritId", merit.getMeritId());
        if(oldMerit == null) {
          return ResUtil.backMeritRes(FAIL_CODE, "更新失败", null);
        } else {
            valuesBucket.putInteger("meritId", merit.getMeritId());
            valuesBucket.putInteger("meritNum", merit.getMeritNum());
            ormContext.update(oldMerit, valuesBucket);
            return ResUtil.backMeritRes(SUCCESS_CODE, "更新成功", null);
        }
    }

    @Override
    public int getVersion() {
        return 1;
    }

    @Override
    public RdbOpenCallback getHelper() {
        return new RdbOpenCallback() {
            @Override
            public void onCreate(RdbStore rdbStore) {
                HiLog.error(LABEL, "创建成功");
            }

            @Override
            public void onUpgrade(RdbStore rdbStore, int i, int i1) {

            }
        };
    }
}
